﻿using System;
using System.Configuration;
using System.Linq;
using System.Net;
using System.Net.Security;

using DTO;

namespace BLL
{
    public static class Ssl
    {
        static System.Configuration.Configuration config =
        ConfigurationManager.OpenExeConfiguration(
          ConfigurationUserLevel.None);
        static AppSettingsSection appSettings = (AppSettingsSection)config.GetSection("appSettings");
        private static string host = DTO.SharedData.HostName;
        private static readonly string[] TrustedHosts = new[] {
            host
        };

        public static void EnableTrustedHosts()
        {
            ServicePointManager.ServerCertificateValidationCallback =
            (sender, certificate, chain, errors) =>
            {
                if (errors == SslPolicyErrors.None)
                {
                    return true;
                }

                var request = sender as HttpWebRequest;
                if (request != null)
                {
                    return TrustedHosts.Contains(request.RequestUri.Host);
                }

                return false;
            };
            
        }
    }
}
